﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;

namespace RssReader.Common
{
    public class RssReaderViewModel : INotifyPropertyChanged
    {

        RssReader reader = new RssReader();

        public RssReaderViewModel()
        {

            //Get the blog source urls
            GetSources();

            this.Title = "Blog Reader";

            //Instantiate the command so that we can bind
            FetchCommand=new DelegateCommand(
                ()=>reader.GetFeed(this.Selected.Url,
                    (posts) => { this.Items = posts.ToList(); Title = this.Selected.Name; }));
        }

        List<RssSource> _sources = new List<RssSource>();
        public List<RssSource> Sources
        {
            get
            {
                return _sources;
            }
        }

        List<RssEntry> _items = new List<RssEntry>();
        public List<RssEntry> Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
                OnPropertyChanged("Items");
            }
        }

        RssSource selected = new RssSource() { Name = "Blog Reader" };
        public RssSource Selected
        {
            get
            {
                return selected;
            }
            set
            {
                selected = value;
                OnPropertyChanged("Selected");
            }
        }

        string title = string.Empty;
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
                OnPropertyChanged("Title");
            }
        }



        public ICommand FetchCommand { get; set; }



        void GetSources()
        {

            _sources.Add(new RssSource()
            {
                Name = "Amazedsaint #Tech Journal",
                Url = "http://feeds.feedburner.com/amazedsaint/articles?format=xml"
            }); 
            
            _sources.Add(new RssSource()
            {
                Name = "MsiGeek's Blog",
                Url = "http://feeds.msigeek.com/msigeek?format=xml"
            });
          


            //http://www.marlabsblogs.com/blog2/?feed=rss2
        }

        void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }



        public event PropertyChangedEventHandler PropertyChanged;
    }


    public class RssSource
    {
        public string Name { get; set; }
        public string Url { get; set; }
    }
}
